﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using System.IO;
using Screenshot2Disk.Localization;

namespace Screenshot2Disk
{
    public class RegistryHelper
    {
        static RegistryKey reg = Registry.CurrentUser;

        const string REGKEY1 = @"Software\Classes\Directory\Background\shell\Screenshot2Disk\";
        const string REGKEY2 = @"Software\Classes\Directory\shell\Screenshot2Disk\";

        public static bool IsRegistered()
        {
            return CheckReg(REGKEY1) && CheckReg(REGKEY2);
        }

        private static bool CheckReg(string regkey)
        {
            var sk = reg.OpenSubKey(regkey);
            if (sk == null)
                return false;

            if (sk.GetValue("") == null)
                return false;

            var sk2 = reg.OpenSubKey(regkey + "command");
            if (sk2 == null)
                return false;

            if (sk2.GetValue("") == null)
                return false;

            return true;
        }

        public static void UnregisterShellExt()
        {
            try
            {
                if (reg.OpenSubKey(REGKEY1) != null)
                    reg.DeleteSubKeyTree(REGKEY1);

                if (reg.OpenSubKey(REGKEY2) != null)
                    reg.DeleteSubKeyTree(REGKEY2);

                Msg.ShowOk(Strings.Success);
            }
            catch (Exception ex)
            {
                Msg.ShowError(ex);
            }
        }

        public static void RegisterShellExt()
        {
            try
            {
                if (reg.OpenSubKey(REGKEY1) != null)
                    reg.DeleteSubKeyTree(REGKEY1);
                if (reg.OpenSubKey(REGKEY2) != null)
                    reg.DeleteSubKeyTree(REGKEY2);

                var currentExe = Environment.GetCommandLineArgs()[0];
                var safeFileName = ToSafeFileName(currentExe);

                var s1 = reg.CreateSubKey(REGKEY1);
                s1.SetValue("", Strings.ContextMenuEntry);
                var s2 = reg.CreateSubKey(REGKEY1 + "command");
                s2.SetValue("", safeFileName + " %V");

                var sk1 = reg.CreateSubKey(REGKEY2);
                sk1.SetValue("", Strings.ContextMenuEntry);
                var sk2 = reg.CreateSubKey(REGKEY2 + "command");
                sk2.SetValue("", safeFileName + " %1");

                Msg.ShowOk(Strings.RegisterSuccessRestart);
            }
            catch (Exception ex)
            {
                Msg.ShowError(ex);
            }
        }

        private static string ToSafeFileName(string currentExe)
        {
            var dirs = currentExe.Split(Path.DirectorySeparatorChar);
            for (int i = 0; i < dirs.Length; i++)
            {
                var dir = dirs[i];
                
                if (dir.Contains(' '))
                {
                    if (dir.Length > 8)
                        dirs[i] = dir.Substring(0, 6) + "~1";
                    else
                        throw new Exception(Strings.DirStructureWhiteSpaces);
                }

            }

            if (dirs.Last().EndsWith(".vshost.exe"))
            {
                dirs[dirs.Length - 1] = dirs.Last().Substring(0, dirs.Last().Length - ".vshost.exe".Length) + ".exe";
            }

            return string.Join(Path.DirectorySeparatorChar.ToString(), dirs);
        }
    }
}
